package cn.wj.school.app.manage;

import cn.wj.school.app.cache.ClientCacheMap;
import cn.wj.school.app.dto.LogoutRespMsg;
import cn.wj.school.app.ui.view.StageView;
import cn.wj.school.common.Constants;
import cn.wj.school.common.controller.StageController;
import cn.wj.school.common.dto.BaseMsg;
import cn.wj.school.common.enums.MsgType;
import cn.wj.school.common.enums.UiFactory;
import cn.wj.school.common.manager.BaseManager;
import io.netty.channel.Channel;
import javafx.scene.input.KeyCombination;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

/**
 * @Author: yueding
 * @Description: 退出处理器
 * @Date: Created in 11:13 2018/12/29
 * @Modified By: yueding Copyright(c) cai-inc.com
 */
public class LogoutManager implements BaseManager {
    @Override
    public String beanName() {
        return MsgType.LOGINOUT.getBeanName();
    }

    @Override
    public void handleMsg(BaseMsg msg, Channel channel) {
        LogoutRespMsg respMsg = (LogoutRespMsg) msg;
        if (respMsg.getSuccess() == Constants.TRUE) {
            ClientCacheMap.putCurrentUser(null);//移除用户
            UiFactory.INSTANCE.runTaskInFxThread(() -> {
                StageController stageController = UiFactory.INSTANCE.getStageController();
                Stage loginStage = stageController.loadStage(StageView.id.LOGIN_VIEW, StageView.layout.LOGIN_VIEW,
                        StageStyle.DECORATED);
                loginStage.setAlwaysOnTop(true);
                loginStage.setResizable(false);
                loginStage.setFullScreen(true);
                loginStage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
                stageController.switchStage(StageView.id.LOGIN_VIEW, StageView.id.MAIN_VIEW);
            });
        }
    }

    @Override
    public BaseMsg getObject() {
        return new LogoutRespMsg();
    }
}
